package com.xiaomeng.singleton.type2;

public class SingletonTest02 {

    public static void main(String[] args) {
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1 == instance2);
        System.out.println(instance1.hashCode());
        System.out.println(instance2.hashCode());
    }


}

class Singleton {
    private static Singleton instance;
    // 构造器私有化
    private Singleton() {

    }
    // 静态代码块中,创建单例对象
    /**
     * 类装载的时候,就执行静态代码块中的代码,初始化类的实例
     * 缺点也是:没法实现懒加载,浪费资源
     * */
    static {
        instance = new Singleton();
    }

    // 提供一个静态方法,返回对象实例
    public static Singleton getInstance() {
        return instance;
    }

}
